from  appium import webdriver
from  selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as  EX
from time import  sleep
import  time




class Base():
    def __init__(self):
        cap = {
            "appium:platformName": "Android",
            "appium:deviceName": "emulator-5554",
            "appium:appPackage": "com.achievo.vipshop",
            "appium:appActivity": "com.achievo.vipshop.homepage.activity.MainActivity",
            "appium:noReset": True
        }
        self.driver= webdriver.Remote('http://127.0.0.1:4723/wd/hub',cap)
        self.driver.implicitly_wait(25)
        sleep(8)

    def loc_ele(self,loc):
        try:
            return self.driver.find_element(*loc)
        except Exception as e:
            print("报错信息：",e)
            print("未定位出元素的变量值",loc)

    def loc_wai_ele(self,loc):
        try:
            return WebDriverWait(self.driver,15).until(EX.presence_of_element_located(loc))
        except Exception as e:
            print("报错信息：",e)
            print("未定位出元素的变量值",loc)


    def loc_ui_ele(self,loc):
        try:
            return  self.driver.find_element_by_android_uiautomator(loc)
        except Exception as e:
            print("报错信息：",e)
            print("未定位出元素的变量值",loc)

    def jieping(self,fir):

        t=time.strftime('%Y%m%d%H%M%S',time.localtime())
        self.driver.save_screenshot(t+fir)

    def dingweiyuans(self,loc,key):
        if type(loc)==str:
            ele = self.loc_ui_ele(loc)
        else:
            ele = self.loc_ele(key)

    def quit(self):
        self.driver.quit()
    def qingqiao(self,x,y):
        self.driver.tap([x,y],500)

    def huadong(self,x,y,x2,y2):
        self.driver.swipe(x,y,x2,y2,500)